package com.jxt.myjxtmanager.ui.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.jxt.myjxtmanager.db.Student;
import com.jxt.myjxtmanager.ui.inter.StudentMsgInter;
import com.jxt.myjxtmanager.ui.model.StudentMsgMo;
import com.jxt.myjxtmanager.views.CustomProgressDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qm on 2017/3/4 0004.
 */
public class StudentMsgPresenter implements StudentMsgMo.OnFindAllDataListener {


    private final CustomProgressDialog dialog;
    private StudentMsgMo msgMo;
    private StudentMsgInter msgInter;

    public StudentMsgPresenter(Context context, StudentMsgInter msgInter) {
        this.msgInter = msgInter;
        msgMo = new StudentMsgMo(context);
        msgMo.setOnFindAllDevicesListener(this);
        dialog = new CustomProgressDialog(context, "");
    }

    public List<Student> getQueryList() {
        return msgMo.getQueryList();
    }

    public void setQueryList(List<Student> msgBeans) {
        msgMo.setQueryList(msgBeans);
    }


    public void dismissDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    public void RequestData() {
        if (dialog != null) {
            dialog.showDialog();
        }
        msgMo.RequestData();
    }


    /**
     * 根据索引模糊查询
     *
     * @param data 所有设备list
     * @param val  EditView的值  模糊查询索引
     * @return 返回模糊查询结果
     */
    public List<Student> getFuzzyQueryFilter(List<Student> data, String val) {
        if (!TextUtils.isEmpty(val.toString())) {

            List<Student> tempDevice = new ArrayList<>();

            String stuNum;//编码
            String stuName;//学生姓名
            String parentName;//家长姓名
            String address;//地址
            String parentPhone;//手机号
            for (Student deviceInfo : data) {
                stuNum = deviceInfo.getStudentNum();
                stuName = deviceInfo.getStudentName();
                parentName = deviceInfo.getParentName();
                address = deviceInfo.getAddress();
                parentPhone = deviceInfo.getParentPhone();
                if ((!TextUtils.isEmpty(stuNum) && stuNum.indexOf(val.toString()) != -1)
                        || (!TextUtils.isEmpty(stuName) && stuName.indexOf(val.toString()) != -1)
                        || (!TextUtils.isEmpty(parentName) && parentName.indexOf(val.toString()) != -1)
                        || (!TextUtils.isEmpty(address) && address.indexOf(val.toString()) != -1)
                        || (!TextUtils.isEmpty(parentPhone) && parentPhone.indexOf(val.toString()) != -1)) {
                    tempDevice.add(deviceInfo);

                } else {
                }

            }
            return tempDevice;
        } else {
            return data;
        }
    }


    @Override
    public void onFindSuccess() {
        msgInter.setMyAdapter();
    }

    @Override
    public void onFailure() {

    }

    @Override
    public void onError() {

    }
}
